********************************************************************************
*** Nowcasting official vacancy statistics (JVWS) with online job postings
********************************************************************************
* Computes MSPEs for different Nowcasts for Figure 2 of Dahlhaus et al. (2025)

version 18 // Stata version used

** PATHS (CHANGE)
global path_output "" // curated data
global path_data "" // aggregate data (included in this package)

********************************************************************************
*** Import and Merge Data
********************************************************************************

*** Import and save JVWS data

import delimited "${path_output}\jvws_naics_month.csv", clear // contains monthly JVWS data

gen mdate=mofd(date) // monthly date variable
format mdate %tm

drop date // original date no longer needed
sort mdate
save, replace

*** Import and merge online job postings

import delimited "${path_output}\indeed_naics_monavg.xlsx", clear // contains monthly online job postings

gen mdate=mofd(date)
format mdate %tm

*** Merge datasets

sort mdate
merge mdate using jvws_naics_month
sort mdate
tsset mdate

********************************************************************************
*** Nowcasting, Loop over all Sectors
********************************************************************************

rename total jvws_100 // rename total JVWS vacancies for loop
rename indeed_total indeed_100 // rename total online job postings for loop

gen t=_n // time variable
   
**** Loop for Nowcast 1: using only JVWS data
foreach j in 11 22 23 31 41 44 48 51 52 53 54 55 56 61 62 71 72 81 91 100  {
    
	gen jvwshat`j'=. // store values for nowcast1 here later

	* start in Nov 2021, split sample in half
	foreach i of num 80/113{
    
	tsset t

	* Regress JVWS of current month on lagged values of JVWS
	reg jvws_`j' l2.jvws_`j' l3.jvws_`j' if mdate > tm(2018m2) & (t<=`i'-2) // start estimations in 2018M3 (2018 Jan, Feb data is noisy)
	predict jvwshat_temp`j' 

	replace jvwshat`j' = jvwshat_temp`j'  if (t==`i')
	drop jvwshat_temp`j'
	
	}

}

**** Loop for Nowcast 2: using JVWS and online job postings
foreach j in 11 22 23 31 41 44 48 51 52 53 54 55 56 61 62 71 72 81 91 100  {

	gen jvwsihat`j'=. // store values for nowcast2 here later

	* start in Nov 2021, split sample in half, note: run until t=115 for plots
	foreach i of num 80/115{
    
	tsset t

	* Regress JVWS of current month on lagged values of JVWS and current and lagged indeed data
	reg jvws_`j' l2.jvws_`j' l3.jvws_`j' indeed_`j' l.indeed_`j' if mdate > tm(2018m2) & (t<=`i'-2) // start estimations in 2018M3 (2018 Jan, Feb data is noisy)
	predict jvwsihat_temp`j' 

	replace jvwsihat`j' = jvwsihat_temp`j'  if (t==`i')
	drop jvwsihat_temp`j'
	}

	* Generate Random Walk forecast as benchmark, run only until t=113 to maintain forecast periods
	gen jvws_`j'rw=.
	replace jvws_`j'rw=l2.jvws_`j' if t<= 133 // generate random walk forecast

}


********************************************************************************
*** Nowcast Evaluations, Collect in Matrices
********************************************************************************
* Note: Code also produces Diebold-Mariano test statistics (not used in AEA paper)

* Initialize the matrices
local s = 20 // number of sectors (including the total)
matrix t1 = J(`s',4,.)	// first element: number of rows (sectors and total), second element: number of columns (sector number, MSE model1, MSE model2, p-value)
matrix t2 = J(`s',4,.)
matrix t3 = J(`s',4,.)

* Loop for tests by sector, and store Diebold-Mariano test 
local i = 1
foreach j in 11 22 23 31 41 44 48 51 52 53 54 55 56 61 62 71 72 81 91 100  {
    
	display `j'
	forv k=1/3 {
		mat t`k'[`i',1] = `j'
	}	
	
	* Compare Nowcast 1 to Nowcast 2
	dmariano  jvws_`j' jvwsihat`j' jvwshat`j',  kernel(bartlett)
	mat t1[`i',2]=`=r(e1bar)'
	mat t1[`i',3]=`=r(e2bar)'
	mat t1[`i',4]=`=r(p)'
	
	* Compare Nowcast 1 to random walk
	dmariano jvws_`j' jvws_`j'rw jvwshat`j',  kernel(bartlett)
	mat t2[`i',2]=`=r(e1bar)'
	mat t2[`i',3]=`=r(e2bar)'
	mat t2[`i',4]=`=r(p)'
	
	* Compare Nocast 2 to random walk
	dmariano jvws_`j' jvws_`j'rw jvwsihat`j',  kernel(bartlett)
	mat t3[`i',2]=`=r(e1bar)'
	mat t3[`i',3]=`=r(e2bar)'
	mat t3[`i',4]=`=r(p)'
	
	* to continue the loop
	local i = `i' + 1
}

preserve

* Store in dataset
clear		// clears data but not matrices

set obs `s'

* Generate the variables
gen sector = .
forv k = 1/3 {
	gen t`k'_model1 = .
	gen t`k'_model2 = .
	gen t`k'_pvalue = .
}

* Fill the variables with the matrices' values
forv i = 1/`s' {
	replace sector = t1[`i',1] if _n==`i'	// take the values in the matrix t1 (the same for t2 and t3)
	forv k = 1/3 {
		replace t`k'_model1 = t`k'[`i',2] if _n==`i'
		replace t`k'_model2 = t`k'[`i',3] if _n==`i'
		replace t`k'_pvalue = t`k'[`i',4] if _n==`i'
	}
}

save "${path_data}\nowcast_mat.dta", replace

restore


